package com.diankangwo.service.impl;

import cn.beecloud.bean.BCOrder;
import com.diankangwo.constant.PayStatus;
import com.diankangwo.constant.PayType;
import com.diankangwo.dao.OnlinePayLogDAO;
import com.diankangwo.dao.OrderMomentPayDAO;
import com.diankangwo.dao.PayManagementDAO;
import com.diankangwo.domain.OnlinePayLog;
import com.diankangwo.domain.PayManagement;
import com.diankangwo.service.OrderMomentPayService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map;

@Service
public class OrderMomentPayServiceImpl implements OrderMomentPayService {

	private static final Logger LOG = Logger.getLogger(OrderMomentPayServiceImpl.class);

	@Autowired
	private OrderMomentPayDAO orderMomentPayDAO; // 实时支付
	@Autowired
	private PayManagementDAO pmd;
	@Autowired
	private OnlinePayLogDAO opld;


	@Override
	public String aliPay(String orderId, Integer totalFee, Map<String, Object> optional) {
		try {
			// 1.根据订单id找到订单相关信息
			PayManagement pm = pmd.queryOrderById(orderId);
			// 2.调用支付
			String billNo = pm.getBillNo();
			String title = pm.getTitle();
			if (!(totalFee == pm.getTotalFee())) {
				return null;
			}
			BCOrder result = orderMomentPayDAO.aliPay(totalFee, billNo, title, optional);
			// 3.记录日志
			OnlinePayLog log = new OnlinePayLog();
			log.setIdKey(result.getObjectId());
			log.setBillNo(billNo);
			log.setTotalFee(totalFee);
			log.setTitle(title);
			log.setPayType(PayType.ALI_WAP.toString());
			log.setReturnType("getUrl");
			log.setPayStatus(PayStatus.unpaid.toString());
			log.setCreateDate(new Date());
			log.setCreateUser("user");
			opld.saveOrUpdate(log);
			return result.getUrl();
		} catch (Exception e) {
			LOG.warn(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public String aliQrPay(String orderId, Integer totalFee) {
		try {
			String billNo = null;
			String title = null;
			return orderMomentPayDAO.aliQrPay(totalFee, billNo, title);
		} catch (Exception e) {
			LOG.warn(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public String aliwapPay(String orderId, Integer totalFee, String returnType) {
		try {
			// 1.根据订单id找到订单相关信息
			PayManagement pm = pmd.queryOrderById(orderId);
			// 2.调用支付
			String billNo = pm.getBillNo();
			String title = pm.getTitle();
			if (!(totalFee == pm.getTotalFee())) {
				return null;
			}
			BCOrder result = orderMomentPayDAO.aliwapPay(totalFee, billNo, title, returnType);
			// 3.记录日志
			OnlinePayLog log = new OnlinePayLog();
			log.setIdKey(result.getObjectId());
			log.setBillNo(billNo);
			log.setTotalFee(totalFee);
			log.setTitle(title);
			log.setPayType(PayType.ALI_WAP.toString());
			log.setReturnType(returnType);
			log.setPayStatus(PayStatus.unpaid.toString());
			log.setCreateDate(new Date());
			log.setCreateUser("system");
			opld.saveOrUpdate(log);
			return result.getUrl();
		} catch (Exception e) {
			LOG.warn(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public String wxqrPay(String orderId, Integer totalFee) {
		try {
			// 1.根据订单id找到订单相关信息
			PayManagement pm = pmd.queryOrderById(orderId);
			// 2.调用支付
			String billNo = pm.getBillNo();
			String title = pm.getTitle();
			if (!(totalFee == pm.getTotalFee())) {

				return null;
			}
			BCOrder result = orderMomentPayDAO.wechatQrsPay(totalFee, billNo, title);
			// 3.记录日志
			OnlinePayLog log = new OnlinePayLog();
			log.setIdKey(result.getObjectId());
			log.setBillNo(billNo);
			log.setTotalFee(totalFee);
			log.setTitle(title);
			log.setPayType(PayType.WX_SCAN.toString());
			log.setReturnType("getUrl");
			log.setPayStatus(PayStatus.unpaid.toString());
			log.setCreateDate(new Date());
			log.setCreateUser("system");
			opld.saveOrUpdate(log);
			return result.getCodeUrl();
		} catch (Exception e) {
			LOG.debug(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public String unionWebPay(String orderId, Integer totalFee) {
		try {
			// 1.根据订单id找到订单相关信息
			PayManagement pm = pmd.queryOrderById(orderId);
			// 2.调用支付
			String billNo = pm.getBillNo();
			String title = pm.getTitle();
			if (!(totalFee == pm.getTotalFee())) {
				return null;
			}
			BCOrder result = orderMomentPayDAO.unionWebPay(totalFee, billNo, title);
			// 3.记录日志
			OnlinePayLog log = new OnlinePayLog();
			log.setIdKey(result.getObjectId());
			log.setBillNo(billNo);
			log.setTotalFee(totalFee);
			log.setTitle(title);
			log.setPayType(PayType.UN_WEB.toString());
			log.setReturnType("getUrl");
			log.setPayStatus(PayStatus.unpaid.toString());
			log.setCreateDate(new Date());
			log.setCreateUser("system");
			opld.saveOrUpdate(log);
			return result.getHtml();
		} catch (Exception e) {
			LOG.debug(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}
	
	@Override
	public String unionWapPay(String orderId, Integer totalFee) {
		try {
			// 1.根据订单id找到订单相关信息
			PayManagement pm = pmd.queryOrderById(orderId);
			// 2.调用支付
			String billNo = pm.getBillNo();
			String title = pm.getTitle();
			if (!(totalFee == pm.getTotalFee())) {
				return null;
			}
			BCOrder result = orderMomentPayDAO.unionWebPay(totalFee, billNo, title);
			// 3.记录日志
			OnlinePayLog log = new OnlinePayLog();
			log.setIdKey(result.getObjectId());
			log.setBillNo(billNo);
			log.setTotalFee(totalFee);
			log.setTitle(title);
			log.setPayType(PayType.UN_WEB.toString());
			log.setReturnType("getUrl");
			log.setPayStatus(PayStatus.unpaid.toString());
			log.setCreateDate(new Date());
			log.setCreateUser("system");
			opld.saveOrUpdate(log);
			return result.getHtml();
		} catch (Exception e) {
			LOG.debug(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

}
